package tests;
import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;


public class LoginPage extends Utils {

    private static String url = "http://127.0.0.1:8080/blog_login.html";

    public LoginPage() {
        super(url);
    }

    //页面能否正常展示
    public void loginPageRight()
    {
        //通过查看只有能正常展示才会有的元素来判断页面加载是否成功
        driver.findElement(By.cssSelector("body > div.container-login > div > h3"));
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));

    }

    //检查登录功能----登录成功
    public void LoginSuc() throws InterruptedException {

        //清空,防止出错
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan"); //输入用户名
        driver.findElement(By.cssSelector("#password")).sendKeys("123456"); //输入密码
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#submit")).click();

        //此时就登录上了,检查一下看看能否找到只有登录成功才有的元素
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")); //写博客 按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")); //退出 按钮
        Thread.sleep(200);
        driver.navigate().back();

    }

    public void LoginFail() throws InterruptedException {

        //通过clear保证输入框没有文本
        //或者使用direver.navigate.refresh() 刷新网页
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan"); //输入用户名
        driver.findElement(By.cssSelector("#password")).sendKeys("123"); //输入密码
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(2000);
        //driver.navigate().refresh();
        Alert alert = driver.switchTo().alert();
        alert.accept();
        driver.navigate().back();
    }
}
